package com.baizhi.controller;

import cn.hutool.captcha.AbstractCaptcha;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ICaptcha;
import cn.hutool.captcha.LineCaptcha;
import com.baizhi.annotation.LogAnnotation;
import com.baizhi.config.ShiroConfig;
import com.baizhi.entity.Admin;
import com.baizhi.service.AdminService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @RequestMapping("login")
    @ResponseBody
    @LogAnnotation(content = "用户登录",type = "login")
    public Map login(String username, String password, String verify){
        Map map = new HashMap();
        Session session = SecurityUtils.getSubject().getSession();
        //获取生成的验证码
        ICaptcha captcha = (ICaptcha) session.getAttribute("captcha");
//        将页面输入的账号和密码封装成令牌
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        System.out.println("captcha = " + captcha);

//        获取主体
        Subject subject = SecurityUtils.getSubject();
        Admin admin = adminService.selectOne(username);
        System.out.println(admin);
        if (captcha.verify(verify)){
            map.put("verify",true);
            try{
//                验证令牌是否正确
                subject.login(token);
                session.setAttribute("admin",admin);
                System.out.println("admin = " + admin);
                map.put("login",true);
//                查看当前主体状态
                boolean b = subject.isAuthenticated();
                System.out.println("b = " + b);
            }catch(Exception e){
                e.printStackTrace();
                map.put("login",false);
                }
            }else{
                map.put("verify",false);
        }
        return map;
    }
    @RequestMapping("verify")
    public void verify(HttpSession session, HttpServletResponse response) throws IOException {
        //生成验证码  实现接口方法
        ICaptcha captcha = CaptchaUtil.createLineCaptcha(200, 100);
        //将生成的验证码对象存入到session对象中
        session.setAttribute("captcha",captcha);
        captcha.write(response.getOutputStream());
        System.out.println("captcha.getCode() = " + captcha.getCode());
    }
    @ResponseBody
    @RequestMapping("insert")
    public Map insert(Admin admin){
        Map map = new HashMap();
        Boolean isAdd = adminService.insert(admin);
        map.put("isAdd",isAdd);
        return map;
    }
    @ResponseBody
    @RequestMapping("update")
    @LogAnnotation(content = "修改用户",type = "updateAdmin")
    public Map update(Admin admin){
        Map map = new HashMap();
        Boolean isUpdate = adminService.update(admin);
        map.put("isUpdate",isUpdate);
        return map;
    }
    @ResponseBody
    @RequestMapping("selectOne")
    public Admin selectOne(String username){
        Admin admin = adminService.selectOne(username);
        return admin;
    }
    @ResponseBody
    @RequestMapping("selectAll")
    @LogAnnotation(content = "查询所有用户",type = "selectAllAdmin")
    public Map selectAll(Integer page,Integer limit){
        Map map = new HashMap();
        System.out.println("page = " + page);
        System.out.println("limit = " + limit);
        PageInfo<Admin> pageInfo = adminService.selectAll(page, limit);
        map.put("data",pageInfo.getList());
        map.put("msg","");
        map.put("code",0);
        map.put("count",pageInfo.getTotal());
        return map;
    }
    @ResponseBody
    @RequestMapping("delete")
    @LogAnnotation(content = "删除用户",type = "deleteAdmin")
    public Map delete(Integer[] ids){
        Map map = new HashMap();
        System.out.println("ids = " + ids);
        Boolean isDelete = adminService.delete(ids);
        map.put("isDelete",isDelete);
        return map;
    }
}
